#include "Tool.h"

  //URL参数解析
string GetValueBuUrl(const string &param,string key)
{
    vector<string> vec_value;
    Split(param,'&',vec_value);
    vector<string> vec_temp;
    for(size_t i = 0; i < vec_value.size() ; i++)
    {
        vec_temp.clear();
        Split(vec_value[i],'=',vec_temp);
        if( 0 == key.compare(vec_temp[0]))
        {
            return vec_temp[1];
        }
    }
    return "";
}
    //URL参数串构造
string GenUrlParamString(const map<string,string> &map_param)
{
    string str_url = "";
    map<string,string>::const_iterator iter = map_param.cbegin();
    for( iter ; iter != map_param.cend() ; ++iter)
    {
        str_url += iter->first + "=" + iter->second + "&";
    }
    if( str_url.size() != 0)
        str_url.pop_back();
    return str_url;
}

void Split(const string& str, char _type,vector<string> &vec_ret)
{
    size_t i_start = 0,i_end = 0;
    for(size_t i = 0;i < str.size(); i++)
    {
        if( str[i] == _type || (i == str.size() - 1) )
        {
            i_end = i;
            size_t i_len = ( i== str.size() -1) ? (i_end-i_start+1) : (i_end-i_start);
            vec_ret.push_back( str.substr(i_start,i_len));
            i_start = i_end + 1;
        }
    }
}

bool SaveFile(string filename, string content)
{
    // 创建一个输出文件流对象，并绑定到指定的文件名  
    ofstream outFile(filename, ios::out | ios::trunc);  
  
    // 检查文件是否成功打开  
    if (!outFile) {  
        cerr << "无法打开文件: " << filename << endl;  
        return false;  
    }  
  
    // 将内容写入文件  
    outFile << content;  
  
    // 关闭文件  
    outFile.close();  
    return true;
}

string ReadFile(string filename)
{
    ifstream inFile(filename);  
  
    //是否成功打开  
    if (!inFile.is_open()) {  
        cerr << "无法打开文件: " << filename << endl;  
        return "";  
    }  

    ostringstream contentStream;  
    contentStream << inFile.rdbuf();   
  
    // 获取字符串流中的内容  
    string content = contentStream.str();  
  
    if (content.empty()) {  
        return "";  
    }  
    
    inFile.close();  
  
    return content;  
}

void EnterMenu()
{
    cout << "\n**************************************"<< endl; 
    cout <<   "***   Chat System V.1.0            ***"<< endl; 
    cout <<   "***   1.登录      2.注册     3.退出  ***"<< endl; 
    cout << "***************************************"<< endl; 
}

void MainMenu()
{
    cout << "\n**********************************"<< endl; 
    cout <<    "***   1.私聊   2.群聊  3.退出   ***"<< endl; 
    cout <<    "*********************************"<< endl; 
}

void GroupMenu()
{
        cout << "\n**********************************************"<< endl; 
        cout <<   "*** 1.创建群聊  2.获取群聊  3.加入群聊  4.退出  ***" << endl;
        cout <<   "**********************************************"<< endl; 
}
